package org.jetnuke.modules.news;

import org.apache.log4j.Logger;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Comments;
import org.jetnuke.entities.MainConfig;
import org.jetnuke.entities.Stories;
import org.jetnuke.util.JetUtil;
import org.jetnuke.util.StringUtils;

public class SubmitComment implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SubmitComment.class);

	int id, pid;

	public void setPid(int pid) {
		this.pid = pid;
	}

	String comment;

	String subject;

	public void setSubject(String subject) {
		if (logger.isDebugEnabled()) {
			logger.debug("setSubject(String) - start"); //$NON-NLS-1$
		}

		this.subject = StringUtils.filterText(subject, true);

		if (logger.isDebugEnabled()) {
			logger.debug("setSubject(String) - end"); //$NON-NLS-1$
		}
	}

	public void setComment(String comment) {
		if (logger.isDebugEnabled()) {
			logger.debug("setComment(String) - start"); //$NON-NLS-1$
		}

		this.comment = StringUtils.filterText(comment, false);

		if (logger.isDebugEnabled()) {
			logger.debug("setComment(String) - end"); //$NON-NLS-1$
		}
	}

	public void setId(int id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		Stories st = DataAccessDriver.getInst().newStoriesDAO()
				.retrieveByPK(id);
		if (st.getACOMM() == 1)
			return null;
		Comments cm = new Comments();

		cm.setCOMMENT(comment);
		cm.setSID(id);
		cm.setPID(pid);
		MainConfig config = (MainConfig) request.getAttribute("MAIN_CONFIG");
		int censorMode = config.getCENSORMODE();
		String censorReplace = config.getCENSORREPLACE();

		subject = StringUtils.filterText(censorMode, censorReplace, subject,
				true);
		cm.setSUBJECT(subject);
		cm.setDATE(new Date());
		if (ui != null)
			cm.setNAME(ui.getID());
		else
			cm.setNAME(1);
		cm.setEMAIL("");
		cm.setHOST_NAME(request.getRemoteAddr());
		cm.setURL("");
		DataAccessDriver.getInst().newCommentDAO().addNew(cm);
		// cm.save();
		DataAccessDriver.getInst().newStoriesDAO().incComments(id);
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/news/story?id=" + id);

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

}
